﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.OA;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.OA.Pages
{
    public partial class DocItem_List : SsTemplatePage
    {
        //缺少的方法: 重命名文件夹  如何下载文件（存放的路径，是否存在库里）   上传文件存放的地址，上传文件是否需要SaveDocItem
        protected void Page_Load(object sender, EventArgs e)
        {
            String pid = Request.QueryString["parentId"] ?? String.Empty;
            if (!IsPostBack)
            {
                BindDocTree();

                AddFolder.Enabled = true;
                AddFolder.NavigateUrl = "DocFolder-Create.aspx?parentId=" + pid;
                if (pid != "")
                {

                    BindDocItem(new Guid(pid));
                    UpLoadDoc.Enabled = true;
                    DelFolder.Enabled = true;
                    EditFolder.Enabled = true;
                    EditFolder.NavigateUrl = "DocFolder-Edit.aspx?parentId=" + pid;
                    UpLoadDoc.NavigateUrl = "DocFile-Upload.aspx?parentId=" + pid;
                }
                else
                {
                    DelFolder.Enabled = false;
                    UpLoadDoc.Enabled = false;
                    EditFolder.Enabled = false;
                }
            }
        }

        public void BindDocItem(TreeNode node, DocItem item)
        {
            OAService server = new OAService();
            List<DocItem> list = server.GetDocItems(item.Id);
            for (int index = 0; index < list.Count; index++)
            {
                if (list[index].TypeCode != DocItem.File)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.Text = list[index].Caption;
                    treeNode.NavigateUrl = "DocItem-List.aspx?ParentId=" + list[index].Id.ToString();

                    node.ChildNodes.Add(treeNode);
                    BindDocItem(treeNode, list[index]);
                }
            }
        }

        public void BindDocTree()
        {
            OAService server = new OAService();
            List<DocItem> list = server.GetDocItems(Guid.Empty);
            for (int index = 0; index < list.Count; index++)
            {
                if (list[index].TypeCode != DocItem.File)
                {
                    TreeNode treeNode = new TreeNode();
                    treeNode.Text = list[index].Caption;
                    Doc_Tree.Nodes.Add(treeNode);
                    BindDocItem(treeNode, list[index]);
                    treeNode.NavigateUrl = "DocItem-List.aspx?ParentId=" + list[index].Id.ToString();
                }
            }
        }

        public void BindDocItem(Guid parentId)
        {
            OAService server = new OAService();
            List<DocItem> list = server.GetDocItems(parentId, DocItem.File);
            rep1.DataSource = list;
            rep1.DataBind();
        }

        protected void rep1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String pid = Request.QueryString["parentId"] ?? String.Empty;
            if (e.CommandName == "Delete")
            {
                OAService server = new OAService();
                string id = e.CommandArgument.ToString();
                ServiceResult result = server.DeleteDocItem(new Guid(id));
                if (result.IsSuccess)
                {
                    Response.Redirect("DocItem-List.aspx?parentId=" + pid);
                }
            }
        }

        protected void DelFolder_Click(object sender, EventArgs e)
        {
            String pid = Request.QueryString["parentId"] ?? String.Empty;
            if (pid == "")
            {
                return;
            }
            else
            {
                OAService server = new OAService();

                List<DocItem> list = server.GetDocItems(new Guid(pid), DocItem.File);
                if (list.Count != 0)
                {
                    Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "del", "alert('存在文件不允许删除.')", true);
                }
                else
                {
                    ServiceResult result = server.DeleteDocItem(new Guid(pid));

                    if (result.IsSuccess)
                    {
                        Response.Redirect("DocItem-List.aspx");
                    }
                }
            }
        }
    }
}